<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
  <link rel="stylesheet" href="./lib/bootstrap.css" />
  <script src="./lib/jquery.js"></script>
  <style>
    .w {
      width: 80%;
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <div class="w">
    <!-- bs3-panel:danger -->
    <div class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title">添加图书</h3>
      </div>
      <div class="panel-body form-inline">
        <!-- bs3input-group:addon -->
        <div class="input-group">
          <div class="input-group-addon">书名</div>
          <input type="text" class="form-control" id="bookname" placeholder="请输入书名" />
        </div>
        <div class="input-group">
          <div class="input-group-addon">作者</div>
          <input type="text" class="form-control" id="author" placeholder="请输入作者" />
        </div>
        <div class="input-group">
          <div class="input-group-addon">出版社</div>
          <input type="text" class="form-control" id="publisher" placeholder="请输入出版社" />
        </div>
        <button id="btnAdd" class="btn btn-danger">添加</button>
      </div>
    </div>

    <table class="table table-bordered table-hover">
      <thead>
        <tr>
          <th>Id</th>
          <th>书名</th>
          <th>作者</th>
          <th>出版社</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="tb">
        <!-- <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td><a href="javascript:void(0);">删除</a></td>
            </tr> -->
      </tbody>
    </table>
  </div>
</body>

</html>
<script>
  $(function () {
    function loadData() {
      $.ajax({
        type: 'get',
        url: 'http://www.liulongbin.top:3006/api/getbooks',
        success: function (backData) {
          // console.log(backData);
          if (backData.status == 200) {
            let arr = backData.data;
            let htmlStr = '';
            $.each(arr, function (index, value) {
              htmlStr += `
            <tr>
                <td>${value.id}</td>
                <td>${value.bookname}</td>
                <td>${value.author}</td>
                <td>${value.publisher}</td>
                <td><a href="javascript:void(0); " data-id="${value.id}">删除</a></td>
            </tr>
            `
            })
            $('#tb').html(htmlStr);
          }
        }

      })
    }
    loadData();
    // 添加书籍
    $('#btnAdd').on('click', function () {
      let bookname = $('#bookname').val().trim();
      let author = $('#author').val().trim();
      let publisher = $('#publisher').val().trim();
      if (bookname == '' || author == '' || publisher == '') {
        return;
      }
      $.ajax({
        type: 'post',
        url: 'http://www.liulongbin.top:3006/api/addbook',
        data: {
          bookname: bookname,
          author: author,
          publisher: publisher
        },
        success: function (backData) {
          if (backData.status == 201) {
            alert(backData.msg);
            loadData();
            $('#bookname').val('');
            $('#author').val('');
            $('#publisher').val('');
          }
        }
      })
    })
    // 删除书籍
    $('#tb').on('click','a',function(){
      let dataId=$(this).attr('data-id');
      $.ajax({
        type:'get',
        url:'http://www.liulongbin.top:3006/api/delbook',
        data:{
          id:dataId
        },
        success:function(backData){
          console.log(backData);
          if(backData.status==200){
            alert(backData.msg);
            loadData();
          }
        }
      })
    })
  })
</script>